package com.example.admin.controller.sys;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.admin.entity.SysOperateLog;
import com.example.admin.service.ISysOperateLogService;
import com.example.util.APIState;
import com.example.util.AjaxResult;
import com.example.util.page.Pager;
import com.example.util.page.SeachParamsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wangkai
 * @since 2020-09-25
 */
@RestController
@Api(value = "操作日志Controller", tags = {"操作日志操作接口"})
@RequestMapping("/sys/sysOperateLog")
public class SysOperateLogController {

    @Autowired
    private ISysOperateLogService service;

    @ApiOperation(value = "操作日志列表", httpMethod = "POST")
    @PostMapping(value = "/page")
    public AjaxResult page(@RequestBody Pager pager) {
        Page<SysOperateLog> page = new Page<>();
        page.setCurrent(pager.getPageNum());
        page.setSize(pager.getPageSize());
        QueryWrapper<SysOperateLog> queryWrapper = new QueryWrapper<>();
        SeachParamsUtil.loadSearchParam(pager, queryWrapper);
        queryWrapper.orderByDesc("oper_time");
        pager.convertPage(service.page(page, queryWrapper));
        return new AjaxResult(APIState.S00, pager);
    }

    @ApiOperation(value = "操作日志刪除", httpMethod = "DELETE")
    @DeleteMapping(value = "/{ids}")
    public AjaxResult delete(@PathVariable Integer[] ids) {
        if (ids != null) {
            if (service.removeByIds(Arrays.asList(ids))) {
                return new AjaxResult(APIState.S00, "删除成功");
            }
        }
        return new AjaxResult(APIState.S01, "删除失败");
    }

    @ApiOperation(value = "操作日志清空", httpMethod = "DELETE")
    @DeleteMapping(value = "/all")
    public AjaxResult delete() {
        QueryWrapper<SysOperateLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("status", 0, 1);
        if (service.remove(queryWrapper)) {
            return new AjaxResult(APIState.S00, "删除成功");
        }
        return new AjaxResult(APIState.S01, "删除失败");
    }

}
